/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edi.dao;

import edi.entities.DepartmentEntity;
import edi.entities.DepartmentUserEntity;

import edi.entities.UserEntity;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class DepartmentUserDaoImp extends HibernateCrudDao<DepartmentUserEntity> implements DepartmentUserDao {
    
    public DepartmentUserDaoImp() {
	super(DepartmentUserEntity.class);
    }

    @Override
    public List<DepartmentUserEntity> findByUser(UserEntity userEntity) {
        return session().createQuery("from DepartmentUserEntity where udeUser = :user")
                .setEntity("user", userEntity).list();
    }

    @Override
    public DepartmentUserEntity findWithoutDepartmentByUser(UserEntity userEntity) {
        return (DepartmentUserEntity) session().createQuery("from DepartmentUserEntity where udeUser = :user and udeDepartment is null")
                .setEntity("user", userEntity).uniqueResult();
    }

    @Override
    public List<DepartmentUserEntity> findWithoutSingleUsersByDepartment(DepartmentEntity departmentEntity) {
        return session()
                .createQuery("from DepartmentUserEntity where udeDepartment = :deparment")
                .setEntity("deparment", departmentEntity).list();
    }
}
